package com.iwechat.aspect;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

/**
 * 记录方法执行日志(debug)
 */
@Aspect
@Component
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class DebugLoggerAspect {
    private final static Logger LOG = LoggerFactory.getLogger(DebugLoggerAspect.class);

    @Pointcut("execution(* com.iwechat..service.*.*.*(..))")
    public void serviceAspect() {
    }

    /**
     * 配置前置通知,使用在方法aspect()上注册的切入点
     */
    @Before("serviceAspect()")
    public void serviceBefore(JoinPoint point) {
        String className = point.getTarget().getClass().getName();
        String method = point.getSignature().getName();
        LOG.debug(className + "." + method + "(" + StringUtils.join(point.getArgs(), ",") + ") start...");
    }

    /**
     * 配置后置通知,使用在方法aspect()上注册的切入点
     */
    @After("serviceAspect()")
    public void serviceAfter(JoinPoint point) {
        String className = point.getTarget().getClass().getName();
        String method = point.getSignature().getName();
        LOG.debug(className + "." + method + "(" + StringUtils.join(point.getArgs(), ",") + ") finshed.");
    }

    @Pointcut("execution(* com.iwechat..controller.*.*.*(..))")
    public void controllerAspect() {
    }

    /**
     * 配置前置通知,使用在方法aspect()上注册的切入点
     */
    @Before("controllerAspect()")
    public void controllerBefore(JoinPoint point) {
        String className = point.getTarget().getClass().getName();
        String method = point.getSignature().getName();
        LOG.debug(className + "." + method + "(" + StringUtils.join(point.getArgs(), ",") + ") start...");
    }

    /**
     * 配置后置通知,使用在方法aspect()上注册的切入点
     */
    @After("controllerAspect()")
    public void controllerAfter(JoinPoint point) {
        String className = point.getTarget().getClass().getName();
        String method = point.getSignature().getName();
        LOG.debug(className + "." + method + "(" + StringUtils.join(point.getArgs(), ",") + ") finshed.");
    }
}
